﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace ggps.SharePoint.ws.Classes
{

    [Serializable()]
    [XmlRoot("Envelope")]
    public class Envelope
    {
        [XmlElement("Header")]
        public Header header { get; set; }
        [XmlElement("Body")]
        public Body body { get; set; }
    }
    [Serializable()]
    [XmlType("Header")]
    public class Header
    {
        // public Security security{ get; set; }
    }
    [Serializable()]
    [XmlType("Security")]
    public class Security
    {
        public UsernameToken usernameToken { get; set; }
    }
    [Serializable()]
    [XmlType("UsernameToken")]
    public class UsernameToken
    {
        public String Username { get; set; }
        public String Password { get; set; }
    }
    [Serializable()]
    [XmlType("Body")]
    public class Body
    {
        // public rgWsPublicVersionInfoResponse versionInfo{ get; set; }
        [XmlElement("rgWsPublicAfmMethodResponse")]
        public rgWsPublicAfmMethodResponse afmMethod { get; set; }
    }
    [Serializable()]
    [XmlType("rgWsPublicVersionInfoResponse")]
    public class rgWsPublicVersionInfoResponse
    {
        public String result { get; set; }
    }
    [Serializable()]
    [XmlType("rgWsPublicAfmMethodResponse")]
    public class rgWsPublicAfmMethodResponse
    {
        [XmlElement("RgWsPublicBasicRt_out")]
        public RgWsPublicBasicRt_out basicInfo { get; set; }

        // public arrayOfRgWsPublicFirmActRt_out firmActivitiesArray { get; set; }
        private List<RgWsPublicFirmActRtUser> _firmActivities = new List<RgWsPublicFirmActRtUser>();
        [XmlArray("arrayOfRgWsPublicFirmActRt_out")]
        public List<RgWsPublicFirmActRtUser> firmActivities { get { return _firmActivities; } set { if (value != null) { _firmActivities = value; };} }

        [XmlElement("firmActivitiesSummary",IsNullable=true)]
        public String firmActivitiesSummary
        {
            set { /* do nothing!*/ }
            get 
            {
                if (firmActivities.Count > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("<ol>");
                    foreach (RgWsPublicFirmActRtUser firmActivity in firmActivities)
                    {
                        sb.Append("<li>");
                        sb.Append(firmActivity.ToHtml());
                        sb.Append("</li>");
                    }
                    sb.Append("</ol>");
                    return sb.ToString();
                }
                return String.Empty;
            }
        }
        [XmlElement("firmActivityMain", IsNullable = true)]
        public String firmActivityMain
        {
            set { /* do nothing!*/ }
            get
            {
                return firmActivityMainFoo(false);
            }
        }

        [XmlElement("firmActivityMainHtml", IsNullable = true)]
        public String firmActivityMainHtml
        {
            set { /* do nothing!*/ }
            get
            {
                return firmActivityMainFoo(true);
            }
        }

        private string firmActivityMainFoo(bool withHtmlTags)
        {
            if (firmActivities.Count > 0)
            {
                try
                {
                    StringBuilder sb = new StringBuilder();
                    if (withHtmlTags) sb.Append("<ol>");
                    RgWsPublicFirmActRtUser firmActivity = firmActivities[0];
                    {
                        if (withHtmlTags) sb.Append("<li>");
                        if (withHtmlTags)
                            sb.Append(firmActivity.ToHtml());
                        else
                            sb.AppendFormat("{0}", firmActivity.firmActDescr);
                        if (withHtmlTags) sb.Append("</li>");
                    }
                    if (withHtmlTags) sb.Append("</ol>");
                    return sb.ToString();
                }
                catch { }
            }
            return String.Empty;
        }

        [XmlElement("pCallSeqId_out")] 
        private Int32 _pCallSeqId_out = 0;
        public Int32? pCallSeqId_out { get { return _pCallSeqId_out; } set { if (value.HasValue) { _pCallSeqId_out = value.Value; };} }

        [XmlElement("pErrorRec_out")]
        public pErrorRec_out err { get; set; }

        public override String ToString()
        {
            try
            {
                return ToHtml();
            }
            catch { }
            return String.Empty;
        }
        public String ToHtml()
        {
            try
            {
                StringBuilder response = new StringBuilder();
                response.Append("<ul>");
                response.Append(basicInfo != null ? String.Format("<li>{0}</li>", basicInfo.ToHtml()) : String.Empty);
                if (firmActivities != null)
                {
                    foreach (RgWsPublicFirmActRtUser activity in firmActivities)
                    {
                        response.Append(firmActivities != null ? String.Format("<li>{0}</li>", activity.ToHtml()) : String.Empty);
                    }
                }
                response.Append(pCallSeqId_out != null ? String.Format("<li>Request ID: {0}</li>", pCallSeqId_out.ToString()) : String.Empty);
                response.Append(err != null ? String.Format("<li>{0}</li>", err.ToHtml()) : String.Empty);
                response.Append("</ul>");
                return response.ToString();
            }
            catch { }
            return String.Empty;
        }
    }

    [Serializable()]
    [XmlType("pErrorRec_out")]
    public class pErrorRec_out
    {
        public String errorDescr { get; set; }
        public String errorCode { get; set; }

        internal string ToHtml()
        {
            try{
                StringBuilder response = new StringBuilder();
                response.Append("<ul>");
                if (!String.IsNullOrEmpty(errorDescr)) response.Append(errorDescr != null ? String.Format("<li>Error: {0}</li>", errorDescr.ToString()) : String.Empty);
                if (!String.IsNullOrEmpty(errorCode)) response.Append(errorCode != null ? String.Format("<li>Error Code: {0}</li>", errorCode.ToString()) : String.Empty);
                response.Append("</ul>");
                return response.ToString();
            }
            catch { }
            return String.Empty;
        }
    }
    [Serializable()]
    [XmlType("RgWsPublicFirmActRtUser")]
    public class RgWsPublicFirmActRtUser : IComparable<RgWsPublicFirmActRtUser>
    {
        public String firmActDescr { get; set; }
        // private Int32 _firmActKind = 0;
        // public Int32? firmActKind { get { return _firmActKind; } set { if (value.HasValue) { _firmActKind = value.Value; };} }
        public String firmActKind { get; set; }

        public String firmActKindDescr { get; set; }
        // private Int32 _firmActCode = 0;
        // public Int32? firmActCode { get { return _firmActCode; } set { if (value.HasValue) { _firmActCode = value.Value; };} }
        public String firmActCode { get; set; }

        public int CompareTo(RgWsPublicFirmActRtUser other)
        {
            if (this.firmActCode == other.firmActCode)
            {
                return other.firmActKind.CompareTo(this.firmActKind);
            }
            return other.firmActCode.CompareTo(this.firmActCode);
        }

        internal string ToHtml()
        {
            try{
                StringBuilder response = new StringBuilder();
                response.Append("<ul>Δραστηριότητα: ");
                if (!String.IsNullOrEmpty(firmActDescr)) 
                    response.Append(firmActDescr != null ? String.Format("<li>{0}</li>", firmActDescr.ToString()) : String.Empty);
                if (!String.IsNullOrEmpty(firmActKind) && 
                    !String.IsNullOrEmpty(firmActKindDescr)) 
                    response.Append(firmActDescr != null ? String.Format("<li>{0}: {1}</li>", firmActKind.ToString(), firmActKindDescr.ToString()) : String.Empty);
                // if (firmActKind.HasValue) if ((firmActKind) > 0) response.Append(firmActKind != null ? String.Format("<li>Δραστηριότητα(προτ.): {0}</li>", firmActKind.ToString()) : String.Empty);
                // if (!String.IsNullOrEmpty(firmActKind)) response.Append(firmActKind != null ? String.Format("<li>Δραστηριότητα(προτ.): {0}</li>", firmActKind.ToString()) : String.Empty);
                // if (!String.IsNullOrEmpty(firmActKindDescr)) response.Append(firmActKindDescr != null ? String.Format("<li>{0}</li>", firmActKindDescr.ToString()) : String.Empty);
                // if (firmActCode.HasValue) if ((firmActCode) > 0) response.Append(firmActCode != null ? String.Format("<li>Δραστηριότητα(κωδ): {0}</li>", firmActCode.ToString()) : String.Empty);
                if (!String.IsNullOrEmpty(firmActCode)) response.Append(firmActCode != null ? String.Format("<li>Kωδ: {0}</li>", firmActCode.ToString()) : String.Empty);
                response.Append("</ul>");
                return response.ToString();
            }
            catch { }
            return String.Empty;
        }
    }
    /*public static class RgWsPublicFirmActRtUser_extensions
    {
        public static List<RgWsPublicFirmActRtUser> Sort(this List<RgWsPublicFirmActRtUser> listOfActivities)
        {
            return listOfActivities.Sort();
        }
    }*/
    [Serializable()]
    [XmlType("RgWsPublicBasicRt_out")]
    public class RgWsPublicBasicRt_out
    {
        public String afm { get; set; } // 094029552// afm>
        public String stopDate { get; set; }
        public String postalAddressNo { get; set; }
        public String doyDescr { get; set; }
        // private Int32 _doy = 0;
        // public Int32? doy { get { return _doy; } set { if (value.HasValue) { _doy = value.Value; };} }
        public String doy { get; set; }
        public String onomasia { get; set; }
        public String legalStatusDescr { get; set; }
        public String registDate { get; set; } //>1970-12-31T00:00:00.000+02:00// registDate>
        // private Int32 _deactivationFlag = 0;
        // public Int32? deactivationFlag { get { return _deactivationFlag; } set { if (value.HasValue) { _deactivationFlag = value.Value; };} }
        public String deactivationFlag { get; set; } // >ΕΝΕΡΓΟΣ ΑΦΜ// deactivationFlagDescr>
        public String deactivationFlagDescr { get; set; } // >ΕΝΕΡΓΟΣ ΑΦΜ// deactivationFlagDescr>
        public String postalAddress { get; set; }// >ΑΛ ΠΑΝΤΟΥ// postalAddress>
        public String firmFlagDescr { get; set; }// >ΕΠΙΤΗΔΕΥΜΑΤΙΑΣ// firmFlagDescr>
        public String commerTitle { get; set; }// >UNISYSTEMS ΑΕ// commerTitle>
        public String postalAreaDescription { get; set; }// >ΚΑΛΛΙΘΕΑ// postalAreaDescription>
        public String INiFlagDescr { get; set; }// >ΜΗ ΦΠ// INiFlagDescr>
        public String postalZipCode { get; set; }// >17671// postalZipCode>

        public String fullAddress { set{} get{
            return String.Format("{0} {1}, {2} {3}",
                postalAddress.Trim(), postalAddressNo.Trim(), postalAreaDescription.Trim(), postalZipCode.Trim());
        }}

        internal string ToHtml()
        {
            try{
                StringBuilder response = new StringBuilder();
                response.Append("Βασικά στοιχεία: <ul>");
                if (!String.IsNullOrEmpty(afm)) response.Append(afm != null ? String.Format("<li>ΑΦΜ: {0}</li>", afm.ToString()) : String.Empty);
                if (!String.IsNullOrEmpty(doyDescr)) response.Append(doyDescr != null ? String.Format("<li>ΔΟΥ: {0}</li>", doyDescr.ToString()) : String.Empty);
                if (!String.IsNullOrEmpty(registDate)) response.Append(registDate != null ? String.Format("<li>Εγγραφή: {0}</li>", registDate.ToString()) : String.Empty);
                if (!String.IsNullOrEmpty(stopDate)) response.Append(stopDate != null ? String.Format("<li>Διακοπή: {0}</li>", stopDate.ToString()) : String.Empty);
                if (!String.IsNullOrEmpty(onomasia)) response.Append(onomasia != null ? String.Format("<li>Επωνυμία: {0}</li>", onomasia.ToString()) : String.Empty);
                if (!String.IsNullOrEmpty(commerTitle)) response.Append(commerTitle != null ? String.Format("<li>Εμπ. Ονομασία: {0}</li>", commerTitle.ToString()) : String.Empty);
                if (!String.IsNullOrEmpty(legalStatusDescr)) response.Append(legalStatusDescr != null ? String.Format("<li>Κατάσταση:{0}</li>", legalStatusDescr.ToString()) : String.Empty);
                // if (deactivationFlag.HasValue) if ((deactivationFlag) > 0) response.Append(deactivationFlag != null ? String.Format("<li>{0}</li>", deactivationFlag.ToString()) : String.Empty);
                if (!String.IsNullOrEmpty(deactivationFlag)) response.Append(deactivationFlag != null ? String.Format("<li>{0}</li>", deactivationFlag.ToString()) : String.Empty);
                if (!String.IsNullOrEmpty(deactivationFlagDescr)) response.Append(deactivationFlagDescr != null ? String.Format("<li>{0}</li>", deactivationFlagDescr.ToString()) : String.Empty);
                if (!String.IsNullOrEmpty(postalAddressNo)) response.Append(postalAddressNo != null ? String.Format("<li>{0}</li>", postalAddressNo.ToString()) : String.Empty);
                if (!String.IsNullOrEmpty(postalAddress)) response.Append(postalAddress != null ? String.Format("<li>Διεύθ.{0}</li>", postalAddress.ToString()) : String.Empty);
                if (!String.IsNullOrEmpty(postalAreaDescription)) response.Append(postalAreaDescription != null ? String.Format("<li>{0}</li>", postalAreaDescription.ToString()) : String.Empty);
                if (!String.IsNullOrEmpty(postalZipCode)) response.Append(postalZipCode != null ? String.Format("<li>ΤΚ.: {0}</li>", postalZipCode.ToString()) : String.Empty);
                if (!String.IsNullOrEmpty(firmFlagDescr)) response.Append(firmFlagDescr != null ? String.Format("<li>{0}</li>", firmFlagDescr.ToString()) : String.Empty);
                if (!String.IsNullOrEmpty(INiFlagDescr)) response.Append(INiFlagDescr != null ? String.Format("<li>{0}</li>", INiFlagDescr.ToString()) : String.Empty);
                response.Append("</ul>");
                return response.ToString();
            }
            catch { }
            return String.Empty;

        }
    }
}
